package com.xja.main;

import com.xja.controller.LwlController;
import com.xja.controller.ZhsController;
import com.xja.entity.*;
import com.xja.entity.pojo.RoomAndBills;
import com.xja.entity.pojo.RoomAndIndent;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @projectName: HotelManager
 * @package: com.xja.main
 * @className: MainTestUi
 * @author: WenLiang0214
 * @description:
 * @date: 2022/5/6 8:41
 */
public class MainTestUi {
    public static void main(String[] args) {
        String userName = null;
        String userPass = null;
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        ZhsController zhsController = new ZhsController();
        LwlController lwlController = new LwlController();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("============客户端页面===============");
            while (true) {
                System.out.println("请选择：1.注册，2.登录");//在此判断用户是登录还是注册
                Integer key = sc.nextInt();
                if (key == 1) {
                    User adduser = new User();
                    System.out.println("姓名");
                    adduser.setUserName(sc.next());
                    System.out.println("电话");
                    adduser.setUserPhone(sc.next());
                    System.out.println("性别");
                    adduser.setUserGender(sc.next());
                    System.out.println("身份证号");
                    adduser.setUserCard(sc.next());
                    System.out.println("密码");
                    adduser.setUserPassWord(sc.next());
                    boolean f = zhsController.addUser(adduser);
                    if (f) {
                        System.out.println("注册成功");
                    } else {
                        System.out.println("注册失败");
                    }
                    break;
                } else if (key == 2) {
                    //在此处登录
                    System.out.println("请输入用户名");
                    userName = sc.next();
                    System.out.println("请输入密码");
                    userPass = sc.next();
                    List<User> AllUser = zhsController.fingAllUser();
                    boolean f = false;
                    for (User user : AllUser) {
                        if (user.getUserName().equals(userName) && user.getUserPassWord().equals(userPass)) {
                            f = true;
                            break;
                        }
                    }
                    if (f) {
                        System.out.println("登录成功");
                        while (true) {
                            //登录成功开始选择功能
                            System.out.println("1.根据价格查询房间");
                            System.out.println("2.客房预定");
                            System.out.println("3.订单管理");
                            System.out.println("4.登记入住");
                            System.out.println("5.结帐退房");
                            System.out.println("6.用户评价");
                            System.out.println("7.退出");
                            System.out.println("请尊敬的客户输入需求");
                            Integer key1 = sc.nextInt();
                            switch (key1) {
                                case 1:
                                    //用户在此查询，根据价格种类，人数，价格，或者模糊查询可住房间
                                    System.out.println("房间价格如下");
                                    List<Room> roomList = lwlController.findRoom();
                                    for (Room room : roomList) {
                                        System.out.println(room.getRoomKind() + "-------" + room.getRoomPrice());
                                    }
                                    System.out.println("请选择所需房间价格");//用户在此查询，根据价格种类，人数，价格，或者模糊查询可住房间
                                    int findRoomByPrice = sc.nextInt();
                                    List<Room> roomListByPrice = lwlController.findRoomByPrice(findRoomByPrice);
                                    for (Room room : roomListByPrice) {
                                            System.out.println(room);
                                    }
                                    break;
                                case 2:
                                    //根据查询结果进行预定
                                    System.out.println("可选房间信息如下");
                                    List<Room> roomList1 = zhsController.findRoom();
                                    for (Room room : roomList1) {
                                        System.out.println("房间号" + room.getRoomId() + "------" + room.getRoomKind() + "-------" + room.getRoomPrice());
                                    }
                                    Indent indent = new Indent();
                                    System.out.println("请选择房间号");
                                    int roomNum = sc.nextInt();
                                    indent.setRoomId(roomNum);
                                    Integer user = zhsController.findUserid(userName);//根据用户名查找用户ID
                                    indent.setUserId(user);//将用户ID插入道indent
                                    System.out.println("请输入入住时间");
                                    String checkTime = sc.next();
                                    Date date = null;
                                    try {
                                        date = sim.parse(checkTime);
                                    } catch (ParseException e) {
                                        e.printStackTrace();
                                    }
                                    indent.setCheckTime(date);
                                    System.out.println("输入入住时长");
                                    indent.setDuratiom(sc.nextInt());
                                    System.out.println("备注");
                                    indent.setRemark(sc.next());
                                    boolean flag = zhsController.indent(indent);
                                    if (flag) {
                                        System.out.println("预定成功");//预定成功，更改房间状态
                                        zhsController.updateroomstatus(roomNum);
                                    } else {
                                        System.out.println("预定失败");
                                    }
                                    continue;
                                case 3:
                                    while (true){
                                    System.out.println("1.查询订单，2.修改订单，3.删除订单");//修改订单，删除订单，查询订单
                                    Integer key2 = sc.nextInt();
                                    switch (key2) {
                                        case 1:
                                            Integer userid = zhsController.findUserid(userName);
                                            List<Indent> indentList = zhsController.selecIndent(userid);
                                            if (indentList.isEmpty()) {
                                                System.out.println("暂无客房预订单");
                                            } else {
                                                for (Indent indent1 : indentList) {
                                                    System.out.println(indent1);
                                                }
                                            }
                                            break;
                                        case 2:
                                            Integer userid5 = zhsController.findUserid(userName);
                                            List<Indent> indentList4 = zhsController.selecIndent(userid5);
                                            if (indentList4.isEmpty()) {
                                                System.out.println("暂无客房预订单可以修改");
                                            }else {
                                            Integer userid2 = zhsController.findUserid(userName);//根据用户名找到ID 用ID查找已定房间状态进行修改
                                            Integer roomid = zhsController.selecRoomId(userid2);
                                            zhsController.deleteIndent(userid2);
                                            zhsController.updateroomstatus1(roomid);
                                            System.out.println("可选房间信息如下");
                                            List<Room> roomList2 = zhsController.findRoom();//查找用户可以选择的房间
                                            for (Room room : roomList2) {
                                                System.out.println("房间号" + room.getRoomId() + "------" + room.getRoomKind() + "-------" + room.getRoomPrice());
                                            }
                                            Indent indent1 = new Indent();
                                            System.out.println("请选择房间号");
                                            int roomNum1 = sc.nextInt();
                                            indent1.setRoomId(roomNum1);
                                            zhsController.updateroomstatus(roomNum1);
                                            Integer userid1 = zhsController.findUserid(userName);//根据用户名查找用户id
                                            indent1.setUserId(userid1);//将用户ID插入道indent
                                            System.out.println("请输入入住时间");
                                            String checkTime1 = sc.next();
                                            Date date1 = null;
                                            try {
                                                date1 = sim.parse(checkTime1);
                                            } catch (ParseException e) {
                                                e.printStackTrace();
                                            }
                                            indent1.setCheckTime(date1);
                                            System.out.println("输入入住时长");
                                            indent1.setDuratiom(sc.nextInt());
                                            System.out.println("备注");
                                            indent1.setRemark(sc.next());
                                            boolean flag1 = zhsController.indent(indent1);
                                            if (flag1) {
                                                System.out.println("更改订单成功");//预定成功，更改房间状态
                                                zhsController.updateroomstatus(roomNum1);
                                            } else {
                                                System.out.println("更改订单失败");
                                            }}
                                            break;
                                        case 3:
                                            Integer userid6 = zhsController.findUserid(userName);
                                            List<Indent> indentList5 = zhsController.selecIndent(userid6);
                                            if (indentList5.isEmpty()) {
                                                System.out.println("暂无客房预订单可以删除，请先预定客房");
                                            }else {
                                            Integer userid3 = zhsController.findUserid(userName);
                                            Integer userid4 = zhsController.findUserid(userName);//根据用户名找到ID 用ID查找已定房间状态进行修改
                                            Integer roomid1 = zhsController.selecRoomId(userid4);
                                            zhsController.updateroomstatus1(roomid1);
                                            boolean f1 = zhsController.deleteIndent(userid3);
                                            if (f1) {
                                                System.out.println("删除成功");
                                            } else {
                                                System.out.println("删除失败，订单库中没有订单");
                                            }}
                                            break;
                                        }System.out.println("是否继续Y/N");
                                    String fl = sc.next();
                                    if ("N".equalsIgnoreCase(fl)) {
                                        break;
                                    }}
                                    continue;
                                case 4:
                                    //入住登记，先查询预定单
                                    //创建登记表对象
                                    Register register = new Register();
                                    List<RoomAndIndent> roomAndIndents = lwlController.fidIndent(userName);
                                    if (roomAndIndents.isEmpty()){
                                        System.out.println("对不起，您还没有订单，不能登记,快去预定吧");
                                        continue;
                                    }else {
                                        System.out.println("您的的预订单信息如下：");
                                        for (RoomAndIndent roomAndIndent : roomAndIndents) {
                                            System.out.println(roomAndIndent.getIndent());
                                            register.setRoomName(roomAndIndent.getRoom().getRoomName());
                                            register.setRoomId(roomAndIndent.getRoom().getRoomId());
                                            register.setRegisterDay(roomAndIndent.getIndent().getDuratiom());
                                            register.setUserId(roomAndIndent.getIndent().getUserId());
                                            register.setInTime(roomAndIndent.getIndent().getCheckTime());
                                        }
                                    }
                                    Integer line = lwlController.register(register);
                                    if (line == null) {
                                        System.out.println("登记失败");
                                    } else {
                                        System.out.println("登记成功");
                                        Integer userid = zhsController.findUserid(userName);
                                        zhsController.deleteIndent(userid);
                                        //房间状态改为0
                                    }
                                    continue;
                                case 5:
                                    //结账退房，根据客户入住房间号进行退房
                                    System.out.println("可以退房的房间号和名称如下");//根据查询结果进行预定
                                    List<Room> roomByStatus = lwlController.findRoomByName(userName);
                                    for (Room byStatus : roomByStatus) {
                                        System.out.println(byStatus.getRoomId() + "---------" + byStatus.getRoomName());
                                    }
                                    System.out.println("请输入房间号");
                                    int roomId = sc.nextInt();
                                    Integer payMoney = lwlController.checkOutById(roomId);
                                    if (payMoney == null) {
                                        System.out.println("退房失败，请重试");
                                    } else {
                                        System.out.println("退房成功,请支付" + payMoney);
                                    }
                                    continue;
                                case 6:
                                    //用户评价
                                    Integer userIdByName = lwlController.findUserIdByName(userName);
                                    List<RoomAndBills> billsById = lwlController.findBillsById(userIdByName);
                                    for (RoomAndBills roomAndBills : billsById) {
                                        System.out.println(roomAndBills.getBills() + "" + roomAndBills.getRoom());
                                    }
                                    if (billsById.isEmpty()) {
                                        System.out.println("您还没有账单，暂时不能评价");
                                    } else {
                                        Integer estimateRoomId = null;
                                        Integer estimateUserId = null;
                                        for (RoomAndBills roomAndBills : billsById) {
                                            estimateRoomId = roomAndBills.getRoom().getRoomId();
                                            estimateUserId = roomAndBills.getBills().getUserId();
                                        }
                                        Comment comment = new Comment();
                                        System.out.println("请输入要评价的内容");
                                        String estimateStr = sc.next();
                                        comment.setRoomId(estimateRoomId);
                                        comment.setContext(estimateStr);
                                        comment.setUserId(estimateUserId);
                                        Integer estimate = lwlController.estimate(comment);
                                        if (estimate == null) {
                                            System.out.println("评价失败");
                                        } else {
                                            System.out.println("评价成功");
                                        }
                                    }
                                    continue;
                                case 7:
                                    System.out.println("退出成功");
                                    System.exit(0);
                                    break;
                                default:
                                    System.out.println("输入有误,请重新输入");
                                    break;
                            }
                            System.out.println("是否继续Y/N");
                            String flag = sc.next();
                            if ("N".equalsIgnoreCase(flag)) {
                                break;
                            }
                        }
                    } else {
                        System.out.println("登录失败");
                        break;
                    }
                } else {
                    System.out.println("输入有误");
                }
            }System.out.println("是否继续Y/N");
            String flag = sc.next();
            if ("N".equalsIgnoreCase(flag)) {
                break;
            }
        }
    }
}